package com.douban.business;

import java.io.UnsupportedEncodingException;

import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;

import com.douban.business.vo.ItemVO;
import com.douban.business.vo.RecordingStatusVO;
import com.douban.constants.DoubanConstant;

public class ListViewRecorder extends Recorder {

	public ListViewRecorder(ItemVO itemvo, RecordingStatusVO rstatus,
			Object element, XMLAttributes attrs, Augmentations augs,
			int elementMode) {
		super(itemvo, rstatus, element, attrs, augs, elementMode);
	}

	/**
	 *Take the sample of code from list view to parse
	 * 
	 * <li id="list5401989" class="item">
	 * <div class="item-show clearfix"> <div class="title"> <a
	 * href="http://book.douban.com/subject/5401989/"> ZYZGC </a> </div> <div
	 * class="date"> 2011-01-25 </div> </div> <div id="grid5401989"
	 * class="hide"> <div class="grid-date"> <span class="intro">XPY / XXCBS /
	 * 2011-1 / 30.00rmb</span><br />
	 * </div> </div></li>
	 */

	protected void recording() {

		// invoked from startElement or endElement
		switch (this.getRecordingMode()) {
		case START_ELEMENT:			
			if (rstatus.isStartListRecording() &&
					attrs.getValue(DoubanConstant.ID.getValue()) != null
					&&attrs.getValue(DoubanConstant.ID.getValue()).length()==11 &&
					attrs.getValue(DoubanConstant.CLASS.getValue())!=null &&
					(attrs.getValue(DoubanConstant.CLASS.getValue()).equalsIgnoreCase(DoubanConstant.ITEM.getValue())
					|| attrs.getValue(DoubanConstant.CLASS.getValue()).equalsIgnoreCase(DoubanConstant.ITEM_LAST.getValue())))
//					&& itemvo.getId() == null
					 {
				// id is inform of "list5401989"
				itemvo = new ItemVO();
				itemvo.setId(attrs.getValue(DoubanConstant.ID.getValue())
						.substring(DoubanConstant.DIGITSTART_POSITION));
				rstatus.clearFieldName();
			}

			if (rstatus.isStartListRecording() &&
					attrs.getValue(DoubanConstant.CLASS.getValue())!=null&&
					attrs.getValue(DoubanConstant.CLASS.getValue()).equalsIgnoreCase(DoubanConstant.TITLE.getValue())) {
				rstatus.setFieldName(DoubanConstant.TITLE.getValue());
			}
			
			if (rstatus.isStartListRecording() &&
					attrs.getValue(DoubanConstant.CLASS.getValue())!=null&&
					attrs.getValue(DoubanConstant.CLASS.getValue()).equalsIgnoreCase(DoubanConstant.INTRO.getValue())) {
				rstatus.setFieldName(DoubanConstant.INTRO.getValue());
			}
			
			if (rstatus.isStartListRecording() &&
					attrs.getValue(DoubanConstant.CLASS.getValue())!=null&&
					attrs.getValue(DoubanConstant.CLASS.getValue()).equalsIgnoreCase(DoubanConstant.DATE.getValue())) {
				rstatus.setFieldName(DoubanConstant.DATE.getValue());
			}
			rstatus.setEleName(element.rawname);
			rstatus.cleanStringBuffer();
			break;
		case END_ELEMENT:
			// it is the same element as start element, which means it is
			if (rstatus.getEleName() != null
					&& rstatus.getStringBuffer() != null
					&& rstatus.getFieldName() != null
					&& element.rawname.equalsIgnoreCase(rstatus.getEleName())) {

				if (element.rawname
						.equalsIgnoreCase(DoubanConstant.ELEMENT_NAME_FOR_TITLE
								.getValue())
						&& rstatus.getFieldName().equalsIgnoreCase(
								DoubanConstant.TITLE.getValue())) {
					// push out the characters recorded to the itemvo
					try {
						itemvo.setTitle(cleanString(new String(rstatus.getStringBuffer(),DoubanConstant.CHAR_SET.getValue())));
					} catch (UnsupportedEncodingException e) {
						itemvo.setIntro("invalid data");
					}
					rstatus.clearEleName();
					rstatus.clearFieldName();
				} else if (element.rawname
						.equalsIgnoreCase(DoubanConstant.ELEMENT_NAME_FOR_DATE
								.getValue())
						&& rstatus.getFieldName().equalsIgnoreCase(
								DoubanConstant.DATE.getValue())) {
					try {
						itemvo.setDate(cleanString(new String(rstatus.getStringBuffer(),DoubanConstant.CHAR_SET.getValue())));
					} catch (UnsupportedEncodingException e) {
						itemvo.setDate("9999-12-12");
					}
					rstatus.clearEleName();
					rstatus.clearFieldName();

				} else if (element.rawname
						.equalsIgnoreCase(DoubanConstant.ELEMENT_NAME_FOR_INTRO
								.getValue())
						&& rstatus.getFieldName().equalsIgnoreCase(
								DoubanConstant.INTRO.getValue())) {
					try {
						itemvo.setIntro(cleanString(new String(rstatus.getStringBuffer(),DoubanConstant.CHAR_SET.getValue())));
					} catch (UnsupportedEncodingException e) {
						itemvo.setIntro("invalid data");
//						should never happen, log will be here later
					}
					rstatus.clearEleName();
					rstatus.clearFieldName();

				}
			}
			if(itemvo.isReady()){
				rstatus.clearRecordingId();
			}
			rstatus.cleanStringBuffer();
			break;
		case CHARACTERS_ELEMENT:
			if (rstatus.getEleName() != null
					&& (rstatus.getEleName().equalsIgnoreCase(
							DoubanConstant.ELEMENT_NAME_FOR_TITLE.getValue())||
						rstatus.getEleName().equalsIgnoreCase(
							DoubanConstant.ELEMENT_NAME_FOR_INTRO.getValue())||
						rstatus.getEleName().equalsIgnoreCase(
							DoubanConstant.ELEMENT_NAME_FOR_DATE.getValue())
									)) {
				// start recording the characters
				rstatus.appendStringBuffer(text.ch, text.offset, text.length);
			}
			break;
		default:
			break;
		}

	}

	private String cleanString(String s) {
		s.replace('\n', '\u0200');
		return s.trim();
	}

}
